package com.demo.apparel.controller.uniapp;

import com.demo.apparel.common.Result;
import com.demo.apparel.dto.uniapp.WxClothesSearchDTO;
import com.demo.apparel.service.uniapp.ClothesBrowseService;
import com.demo.apparel.service.uniapp.WxCategoryService;
import com.demo.apparel.utils.JwtUtils;
import com.demo.apparel.vo.ClothesCategoryVO;
import com.demo.apparel.vo.PageVO;
import com.demo.apparel.vo.uniapp.CategoryBrowseVO;
import com.demo.apparel.vo.uniapp.SimpleClothesVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/wx/category")
@Tag(name = "衣物分类接口", description = "我的衣橱信息")
public class ClothesBrowseController {

    @Autowired
    private  ClothesBrowseService clothesBrowseService;

    @Autowired
    private JwtUtils jwtUtils;

    @Autowired
    private WxCategoryService wxCategoryService;

    /**
     *
     * @return
     */
    @GetMapping("/group")
    @Operation(summary = "获取所有用户上传的衣物数据")
    public Result<List<ClothesCategoryVO>> getClothesGroup() {
        return Result.success(clothesBrowseService.getClothesGroupedByCategory());
    }

    /**
     *获取当前用户的上传衣物分类的数据
     * @param request
     * @return
     */
    @GetMapping("/list")
    @Operation(summary = "获取当前用户上传的衣物数据")
    public Result list(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        if (token != null && token.startsWith("Bearer ")) {
            token = token.substring(7); // 去除 Bearer
        }
        String userId = jwtUtils.getUserId(token);

        List<CategoryBrowseVO> data = clothesBrowseService.getUserClothesByCategory(userId);
        return Result.success(data);
    }

    /**
     *
     * @param keyword
     * @param season
     * @param request
     * @return
     */
    @GetMapping("/search")
    @Operation(summary = "get进行查询衣物")
    public Result<List<SimpleClothesVO>> searchClothes(
            @RequestParam String keyword,
            @RequestParam(required = false) String season,
            HttpServletRequest request) {

        String token = request.getHeader("Authorization");
        if (token != null && token.startsWith("Bearer ")) {
            token = token.substring(7); // 去除 Bearer
        }
        String userId = jwtUtils.getUserId(token);

        List<SimpleClothesVO> clothes = clothesBrowseService.searchUserClothes(userId, keyword, season);
        return Result.success(clothes);
    }


    /**
     *
     * @param dto
     * @return
     */
    @PostMapping("/searchPage")
    @Operation(summary = "post方式分页查询衣物")
    public Result<PageVO<SimpleClothesVO>> searchPage(@RequestBody WxClothesSearchDTO dto,
                                                      HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        if (token != null && token.startsWith("Bearer ")) {
            token = token.substring(7); // 去掉 Bearer 前缀
        }
        String userId = jwtUtils.getUserId(token);

        PageVO<SimpleClothesVO> result = clothesBrowseService.searchUserClothesPage(
                userId,
                dto.getKeyword(),
                dto.getSeason(),
                dto.getPageNum(),
                dto.getPageSize()
        );

        return Result.success(result);
    }



    /**
     * 分页搜索所有用户的衣服（支持typeName关键字、description关键字和season筛选）
     */
    @PostMapping("/searchAll")
    @Operation(summary = "post获取所有用户的衣服")
    public Result<PageVO<SimpleClothesVO>> searchAllUsersClothes(
            @RequestBody WxClothesSearchDTO dto) {

        PageVO<SimpleClothesVO> result = clothesBrowseService.searchAllUsersClothesPage(
                dto.getKeyword(),
                dto.getSeason(),
                dto.getDescription(),
                dto.getPageNum(),
                dto.getPageSize()
        );

        return Result.success(result);
    }

    @PostMapping("/searchUserClothes")
    @Operation(summary = "post获取当前用户的衣服")
    public Result<PageVO<SimpleClothesVO>> searchUserClothes(@RequestBody WxClothesSearchDTO dto, HttpServletRequest request) {

        String token = request.getHeader("Authorization");
        if (token != null && token.startsWith("Bearer ")) {
            token = token.substring(7); // 去掉 Bearer 前缀
        }
        String userId = jwtUtils.getUserId(token);

        PageVO<SimpleClothesVO> result = clothesBrowseService.searchUserClothesPagees(
                userId,
                dto.getKeyword(),
                dto.getSeason(),
                dto.getDescription(),
                dto.getPageNum(),
                dto.getPageSize()
        );

        return Result.success(result);
    }

    @GetMapping("/map")
    @Operation(summary = "获取大类名称")
    public Result<Map<String, String>> getCategoryMap() {
        return Result.success(wxCategoryService.getCategoryMap());
    }

    @GetMapping("/MapList")
    public Result<List<Map<String, String>>> list() {
        List<Map<String, String>> data = wxCategoryService.listForSelect();
        return Result.success(data); // 假设你有统一响应类 R
    }
}

